﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Physio10Context;
using DatabaseManager;

namespace Physio_Clinic
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["currentDoctor"] != null && !Page.IsPostBack)
            {
                Doctor doctor = (Doctor)Session["currentDoctor"];

                DoctorName.Text = doctor.DoctorName;
                DoctorSurName.Text = doctor.DoctorSurName;
                AddressTxt.Text = doctor.Address;
                MobileTxt.Text = doctor.Mobile;
                EmailTxt.Text = doctor.Email;

                WorkDropDownList.SelectedValue = doctor.WorkDay.ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {


            //lbl.Text = MyFile.PostedFile.FileName;
            //MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");             
            //MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");
            //FileUpload.SaveAs(Server.MapPath("../images/image.gif"))
            //FileUpload.SaveAs(Server.MapPath("../images") & "\image.gif")
            //MyFile.PostedFile.SaveAs(Server.MapPath("../DoctorImage/" + MyFile.PostedFile.FileName));

            Doctor doctor = null;

            if (Session["currentDoctor"] != null)
            {
                doctor = (Doctor)Session["currentDoctor"];
            }
            else
            {
                doctor = new Doctor();
            }


            if (MyFile.PostedFile.ContentLength != 0)
            {
                string extension = System.IO.Path.GetExtension(MyFile.PostedFile.FileName);
                //MyFile.PostedFile.SaveAs("c:\\UploadFile\\" + doctor.DoctorId + ".jpg");
                //MyFile.PostedFile.SaveAs("~/DoctorImage/" + doctor.DoctorId.ToString() + ".jpg");
                //MyFile.PostedFile.SaveAs(Server.MapPath("../DoctorImage/" + doctor.DoctorId.ToString() + ".jpg"));
                //MyFile.PostedFile.SaveAs("~\\DoctorImage\\" + doctor.DoctorId.ToString() + ".jpg");
                string trailingPath = "DoctorImage\\" + doctor.DoctorId.ToString() + ".jpg";
                MyFile.PostedFile.SaveAs(System.IO.Path.Combine(Server.MapPath(" "), trailingPath));
            }

            doctor.DoctorName = DoctorName.Text;
            doctor.DoctorSurName = DoctorSurName.Text;
            doctor.Address = AddressTxt.Text;
            doctor.Mobile = MobileTxt.Text;
            doctor.Email = EmailTxt.Text;

            doctor.WorkDay = Convert.ToInt32(WorkDropDownList.SelectedValue.ToString());


            if (Session["currentDoctor"] == null)
            {
                PersistentManager.Instance.Add(doctor);
            }


            PersistentManager.Instance.Save();
            Session["currentDoctor"] = null;
            Response.Redirect("Doctor.aspx");
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            Response.Redirect("Doctor.aspx");
        }

        protected void UploadBtn_Click(object sender, EventArgs e)
        {
            if (MyFile.PostedFile.ContentLength == 0)
            {

                //lbl.Text = "Cannot upload zero length file";

                return;

            }

            //lbl.Text = MyFile.PostedFile.FileName;

            //MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");             
            //MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");
            //FileUpload.SaveAs(Server.MapPath("../images/image.gif"))
            //FileUpload.SaveAs(Server.MapPath("../images") & "\image.gif")
            MyFile.PostedFile.SaveAs(Server.MapPath("../DoctorImage/" + MyFile.PostedFile.FileName));
        }

    }
}